home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 7: Sunsite / Linux Cubed Series 7 - Sunsite Vol 1.iso / system / admin / pwdutils.00 / pwdutils / pwdutils-1.00 / readpwd.c < prev    next >
C/C++ Source or Header  |  1996-05-07  |  3KB  |  120 lines

  1. #include <pwd.h>
  2. #include <time.h>
  3. #include <ctype.h>
  4. #include <stdio.h>
  5. #include <string.h>
  6. #include <unistd.h>
  7. #include <sys/types.h>
  8.  
  9. #include "pwdutils.h"
  10.  
  11. #define bin_to_ascii(c) ((c)>=38?((c)-38+'a'):(c)>=12?((c)-12+'A'):(c)+'.')
  12.  
  13. char *getoldpwd(struct passwd *pwd)
  14. {
  15.   static char oldpwd[20];
  16.   
  17.   strncpy(oldpwd,getpass("Enter old password: "),sizeof(oldpwd)-1);
  18.   if(strncmp(pwd->pw_passwd, crypt(oldpwd, pwd->pw_passwd), 13)) 
  19.     {
  20.       fprintf(stderr,"Wrong password.\n");
  21.       exit(1);
  22.     }  
  23.   return oldpwd;
  24. }
  25.  
  26. char *getnewpwd(struct passwd *pwd)
  27. {
  28.   char *pwdstr = NULL, *cryptstr, *p, *q;
  29.   int ucase, lcase, other, r;
  30.   char pwdstr1[10];
  31.   char salt[2];
  32.   uid_t gotuid;
  33.   time_t tm;
  34.  
  35.   gotuid = getuid();
  36.  
  37. redo_it:
  38.   pwdstr = getpass("Enter new password: ");
  39.   if (pwdstr[0] == '\0' && gotuid != 0) 
  40.     {
  41.       printf("Password not changed.");
  42.       exit(1);
  43.     }
  44.   
  45.   if((strlen(pwdstr) < 6) && gotuid) 
  46.     {
  47.       printf("The password must have at least 6 characters, try again.");
  48.       goto redo_it;
  49.     }
  50.   
  51.   other = ucase = lcase = 0;
  52.   for(p = pwdstr; *p; p++) {
  53.     ucase = ucase || isupper(*p);
  54.     lcase = lcase || islower(*p);
  55.     other = other || !isalpha(*p);
  56.   }
  57.   
  58.   if((!ucase || !lcase) && !other && gotuid) 
  59.     {
  60.       printf("The password must have both upper- and lowercase");
  61.       printf("letters, or non-letters; try again.");
  62.       goto redo_it;
  63.     }
  64.   
  65.   if (pwd->pw_passwd[0] 
  66.       && !strncmp(pwd->pw_passwd, crypt(pwdstr, pwd->pw_passwd), 13)
  67.       && gotuid) 
  68.     {
  69.       printf("You cannot reuse the old password.");
  70.       goto redo_it;
  71.     }
  72.   
  73.   r = 0;
  74.   for(p = pwdstr, q = pwd->pw_name; *q && *p; q++, p++) 
  75.     {
  76.       if(tolower(*p) != tolower(*q)) 
  77.     {
  78.       r = 1;
  79.       break;
  80.     }
  81.     }
  82.   
  83.   for(p = pwdstr + strlen(pwdstr)-1, q = pwd->pw_name;
  84.       *q && p >= pwdstr; q++, p--) 
  85.     {
  86.       if(tolower(*p) != tolower(*q)) 
  87.     {
  88.       r += 2;
  89.       break;
  90.     }
  91.     }
  92.   
  93.   if(gotuid && r != 3) 
  94.     {
  95.       puts("Please don't use something like your username as password!");
  96.       goto redo_it;
  97.     }
  98.   
  99.   /* do various other checks for stupid passwords here... */
  100.   
  101.   strncpy(pwdstr1, pwdstr, 9);
  102.   pwdstr = getpass("Re-type new password: ");
  103.   
  104.   if(strncmp(pwdstr, pwdstr1, 8)) 
  105.     {
  106.       puts("You misspelled it. Password not changed.");
  107.       exit(1);
  108.     }
  109.   
  110.   time(&tm);
  111.   salt[0] = bin_to_ascii(tm & 0x3f);
  112.   salt[1] = bin_to_ascii((tm >> 6) & 0x3f);
  113.   cryptstr = crypt(pwdstr, salt);
  114.  
  115.   if (pwdstr[0] == 0) cryptstr = "";
  116.   
  117.   return cryptstr;
  118.  
  119. }
  120.